home *** CD-ROM | disk | FTP | other *** search
/ Winzipper / Winzipper_ISO.iso / programming / oracle7 7.2 / OCI72 / WINOCI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-25  |  36.2 KB  |  1,010 lines

  1. /*
  2.  * winoci.c
  3.  *
  4.  * Example OCI Application for Windows V3.0.
  5.  *
  6.  * by D. Colello -- Desktop Products Division.
  7.  *
  8.  * Copyright 1991, Oracle Corporation.
  9.  *
  10.  * Modified -  
  11.  *    Majkut       10/25/95 -  Really replace calls to osql3 with oparse - bug 316375
  12.  *    cchui       10/25/94 -  replace calls to osql3 with oparse - bug 229431
  13.  *    cchui       10/25/94 -  replace call to olon with orlon - bug 229431
  14.  *  dcriswel   12/03/93 -  Port to v7
  15.  *   Colello    07/09/91 - Created from winsam.pc
  16.  */
  17.  
  18. #include <windows.h>
  19. #include "winoci.h"
  20. #include <string.h>
  21. #include "ociapr.h"
  22.  
  23. /********************************************************************/
  24. /*                 Oracle Declaration Section                       */
  25. /********************************************************************/
  26.  
  27.     /* Cursor and LDA for communicating with ORACLE */
  28.     struct cda_def lda;                                        /* lda area */
  29. #define  LDA (struct cda_def *)&lda
  30.     struct cda_def sel_curs;                          /* cursor for SELECT */
  31. #define  SEL_CURS  (struct cda_def *)&sel_curs
  32.     struct cda_def dml_curs;      /* cursor for DML...insert,update,delete */
  33. #define  DML_CURS  (struct cda_def *)&dml_curs
  34.  
  35.     /* Input host variables for logging into ORACLE. */
  36.     char connectstr [ MAX_CONNECT + 1 ];
  37.     char userid[ MAX_USERID + 1 ];                  /*used at CONNECT time*/
  38.     char password[ MAX_PASSWORD + 1 ];              /*used at CONNECT time*/
  39.  
  40.     /* Input/Output host variables for EMP table. */
  41.     char emp_empno[ 5 ];     /*used for EMP table*/
  42.     char emp_ename[ 11 ];    /*used for EMP table*/
  43.     char emp_job[ 10 ];      /*used for EMP table*/
  44.     char emp_mgr[ 5 ];       /*used for EMP table*/
  45.     char emp_hiredate[ 10 ]; /*used for EMP table*/
  46.     char emp_sal[ 9 ];       /*used for EMP table*/
  47.     char emp_comm[ 9 ];      /*used for EMP table*/
  48.     char emp_deptno[ 3 ];    /*used for EMP table*/
  49.  
  50.     /* Null field indicators for table EMP. */
  51.     short ind_emp_empno;           /*used for EMP table*/
  52.     short ind_emp_ename;           /*used for EMP table*/
  53.     short ind_emp_job;             /*used for EMP table*/
  54.     short ind_emp_mgr;             /*used for EMP table*/
  55.     short ind_emp_hiredate;        /*used for EMP table*/
  56.     short ind_emp_sal;             /*used for EMP table*/
  57.     short ind_emp_comm;            /*used for EMP table*/
  58.     short ind_emp_deptno;          /*used for EMP table*/
  59.  
  60.     /* Structure to keep info on current session */
  61.     struct oracle_session session;
  62.  
  63.     char hda[256];
  64.  
  65. /********************************************************************/
  66. /*                   Windows Declaration Section                    */
  67. /********************************************************************/
  68.  
  69. static HANDLE hInst;
  70.  
  71. /********************************************************************/
  72. /*                   OCI and Windows Code.                          */
  73. /********************************************************************/
  74.  
  75. /*
  76.  *ProcessOracleErrorCode
  77.  */
  78. BOOL ProcessOracleErrorCode(hWnd, csrlda)
  79. HWND hWnd;
  80. struct cda_def  *csrlda;
  81. {
  82.     char buffer[ 133 ];               /* buffer to hold Oracle error message */
  83.  
  84.     if (csrlda->rc) {                         /* if error code is set... */
  85.  
  86.        /* Get Oracle Error Message */
  87.        oerhms(LDA, (sb2)csrlda->rc, (char  *)buffer, sizeof(buffer)-1);
  88.  
  89.        /* Print Oracle Error */
  90.        MessageBox(hWnd, buffer, "ORACLE Error", MB_OK);
  91.        return FALSE;
  92.  
  93.        } /*if*/
  94.  
  95.     return TRUE;
  96.  
  97. } /*ProcessOracleErrorCode*/
  98.  
  99. /*
  100.  *AboutDlg
  101.  */
  102. BOOL CALLBACK AboutDlg(hDlg, message, wParam, lParam)
  103. HWND hDlg;
  104. unsigned message;
  105. WORD wParam;
  106. LONG lParam;
  107. {
  108.  
  109.     switch (message) {
  110.  
  111.         case WM_COMMAND :
  112.             EndDialog(hDlg, TRUE);
  113.             return TRUE;
  114.  
  115.         case WM_INITDIALOG :
  116.             return TRUE;
  117.  
  118.         default :
  119.             return FALSE;
  120.         } /*switch*/
  121.  
  122.     return FALSE;
  123.  
  124. } /*AboutDlg*/
  125.  
  126. /*
  127.  *GetEmpRecord
  128.  */
  129. PROCEDURE GetEmpRecord(hDlg)
  130. HWND hDlg;
  131. {
  132.  
  133.     /* clear indicator variables */
  134.     ind_emp_empno = ind_emp_ename = ind_emp_job = ind_emp_mgr =
  135.       ind_emp_hiredate = ind_emp_sal = ind_emp_comm = ind_emp_deptno = 0;
  136.  
  137.     /* Get input from Dialog Box */
  138.     /* Set indicator variables if empty string */
  139.     if (GetDlgItemText(hDlg, ID_EMPLOYEES_FLD_EMPNO,
  140.                        emp_empno, sizeof(emp_empno)) == 0)
  141.         ind_emp_empno = -1;
  142.  
  143.     if (GetDlgItemText(hDlg, ID_EMPLOYEES_FLD_ENAME,
  144.                        emp_ename, sizeof(emp_ename)) == 0)
  145.         ind_emp_ename = -1;
  146.  
  147.     if (GetDlgItemText(hDlg, ID_EMPLOYEES_FLD_JOB,
  148.                        emp_job, sizeof(emp_job)) == 0)
  149.         ind_emp_job = -1;
  150.  
  151.     if (GetDlgItemText(hDlg, ID_EMPLOYEES_FLD_MGR,
  152.                        emp_mgr, sizeof(emp_mgr)) == 0)
  153.         ind_emp_mgr = -1;
  154.  
  155.     if (GetDlgItemText(hDlg, ID_EMPLOYEES_FLD_HIREDATE,
  156.                        emp_hiredate, sizeof(emp_hiredate)) == 0)
  157.         ind_emp_hiredate = -1;
  158.  
  159.     if (GetDlgItemText(hDlg, ID_EMPLOYEES_FLD_SAL,
  160.                        emp_sal, sizeof(emp_sal)) == 0)
  161.         ind_emp_sal = -1;
  162.  
  163.     if (GetDlgItemText(hDlg, ID_EMPLOYEES_FLD_COMM,
  164.                        emp_comm, sizeof(emp_comm)) == 0)
  165.         ind_emp_comm = -1;
  166.  
  167.     if (GetDlgItemText(hDlg, ID_EMPLOYEES_FLD_DEPTNO,
  168.                        emp_deptno, sizeof(emp_deptno)) == 0)
  169.         ind_emp_deptno = -1;
  170.  
  171.     return;
  172.  
  173. } /*GetEmpRecord*/
  174.  
  175. /*
  176.  *SetEmpRecord
  177.  */
  178. PROCEDURE SetEmpRecord(hDlg)
  179. HWND hDlg;
  180. {
  181.  
  182.     /* Set fields in Dialog Box to current values of host variables */
  183.     SetDlgItemText(hDlg, ID_EMPLOYEES_FLD_EMPNO, emp_empno);
  184.     SetDlgItemText(hDlg, ID_EMPLOYEES_FLD_ENAME, emp_ename);
  185.     SetDlgItemText(hDlg, ID_EMPLOYEES_FLD_JOB, emp_job);
  186.     SetDlgItemText(hDlg, ID_EMPLOYEES_FLD_MGR, emp_mgr);
  187.     SetDlgItemText(hDlg, ID_EMPLOYEES_FLD_HIREDATE, emp_hiredate);
  188.     SetDlgItemText(hDlg, ID_EMPLOYEES_FLD_SAL, emp_sal);
  189.     SetDlgItemText(hDlg, ID_EMPLOYEES_FLD_COMM, emp_comm);
  190.     SetDlgItemText(hDlg, ID_EMPLOYEES_FLD_DEPTNO, emp_deptno);
  191.  
  192.     return;
  193.  
  194. } /*SetEmpRecord*/
  195.  
  196. /*
  197.  *EmployeesDlg
  198.  */
  199. BOOL CALLBACK EmployeesDlg(hDlg, message, wParam, lParam)
  200. HWND hDlg;
  201. unsigned message;
  202. WORD wParam;
  203. LONG lParam;
  204. {
  205.     /* SQL statement used to get info from database */
  206.     char  *empsel="SELECT EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, \
  207.                       DEPTNO FROM EMP FOR UPDATE OF EMPNO, ENAME, JOB, MGR, \
  208.                       HIREDATE, SAL, COMM, DEPTNO";
  209.         /*****************************************************************/
  210.         /* You need the "FOR UPDATE OF" clause so that you can use       */
  211.         /* the "rowid" field with UPDATE and DELETE statements. This also*/
  212.         /* applies to a PRO*C 'WHERE CURRENT OF' clause. This field will */
  213.         /* be valid for other statements to use if and only if you       */
  214.         /* use the "FOR UPDATE OF" clause with the SELECT since this     */
  215.         /* locks the ROWID until the next COMMIT [see SQL manuals].      */
  216.         /*****************************************************************/
  217.  
  218.     /* SQL statement to delete currently fetched emp record */
  219.     char  *delete="DELETE FROM EMP WHERE ROWID = :row_id";
  220.  
  221.     /* SQL statement to update currently fetched emp record */
  222.     char  *update="UPDATE EMP SET EMPNO=:empno, ENAME=:ename, JOB=:job, \
  223.                       MGR=:mgr, HIREDATE=:hiredate, SAL=:sal, COMM=:comm, \
  224.                       DEPTNO=:deptno WHERE ROWID = :row_id";
  225.  
  226.     /* SQL statement to insert an emp record */
  227.     char  *insert="INSERT INTO EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM, \
  228.                       DEPTNO) VALUES (:empno,:ename,:job,:mgr,:hiredate,:sal, \
  229.                       :comm,:deptno)";
  230.  
  231.     switch (message) {
  232.  
  233.         case WM_COMMAND :
  234.  
  235.             switch (wParam) {
  236.  
  237.                 case ID_EMPLOYEES_EXIT :
  238.                     EndDialog(hDlg, 1);
  239.                     break;
  240.  
  241.                 case ID_EMPLOYEES_SELECT :
  242.  
  243.                   /* check to see if we have an open, parsed, defined cursor */
  244.                   /* if so, all we need to do below is re-execute the query
  245.                      in order to get a new active set.  Otherwise, open,
  246.                      parse, and define output buffers for the query */
  247.  
  248.                     if (session.opened == FALSE) {
  249.  
  250.                        /* open a cursor for the select from emp */
  251.                        if (oopen(SEL_CURS, LDA, (char  *)0,
  252.                               -1, -1, (char  *)0, -1))  {
  253.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  254.                           break;
  255.                           } /*if*/
  256.  
  257.                        /* parse cursor */
  258.                        if (oparse(SEL_CURS, (char  *)empsel, (sb4)-1, (sword)0, (ub4)1)) {
  259.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  260.                           break;
  261.                           } /*if*/
  262.  
  263.                        /* define output buffers for the all fields
  264.                           in the select list of our query */
  265.                        if (odefin(SEL_CURS, 1, (char  *)emp_empno,
  266.                              sizeof(emp_empno),
  267.                              NULLTERM, -1, (short  *)&ind_emp_empno,
  268.                              (char  *)0, -1, -1, (short  *)0,
  269.                              (short  *)0)) {
  270.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  271.                           break;
  272.                           } /*if*/
  273.  
  274.                        if (odefin(SEL_CURS, 2, (char  *)emp_ename,
  275.                              sizeof(emp_ename),
  276.                              NULLTERM, -1, (short  *)&ind_emp_ename,
  277.                              (char  *)0, -1, -1, (short  *)0,
  278.                              (short  *)0)) {
  279.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  280.                           break;
  281.                           } /*if*/
  282.  
  283.                        if (odefin(SEL_CURS, 3, (char  *)emp_job,
  284.                              sizeof(emp_job),
  285.                              NULLTERM, -1, (short  *)&ind_emp_job,
  286.                              (char  *)0, -1, -1, (short  *)0,
  287.                              (short  *)0)) {
  288.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  289.                           break;
  290.                           } /*if*/
  291.  
  292.                        if (odefin(SEL_CURS, 4, (char  *)emp_mgr,
  293.                              sizeof(emp_mgr),
  294.                              NULLTERM, -1, (short  *)&ind_emp_mgr,
  295.                              (char  *)0, -1, -1, (short  *)0,
  296.                              (short  *)0)) {
  297.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  298.                           break;
  299.                           } /*if*/
  300.  
  301.                        if (odefin(SEL_CURS, 5,
  302.                              (char  *)emp_hiredate,
  303.                              sizeof(emp_hiredate),
  304.                              NULLTERM, -1, (short  *)&ind_emp_hiredate,
  305.                              (char  *)0, -1, -1, (short  *)0,
  306.                              (short  *)0)) {
  307.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  308.                           break;
  309.                           } /*if*/
  310.  
  311.                        if (odefin(SEL_CURS, 6, (char  *)emp_sal,
  312.                              sizeof(emp_sal),
  313.                              NULLTERM, -1, (short  *)&ind_emp_sal,
  314.                              (char  *)0, -1, -1, (short  *)0,
  315.                              (short  *)0)) {
  316.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  317.                           break;
  318.                           } /*if*/
  319.  
  320.                        if (odefin(SEL_CURS, 7, (char  *)emp_comm,
  321.                              sizeof(emp_comm),
  322.                              NULLTERM, -1, (short  *)&ind_emp_comm,
  323.                              (char  *)0, -1, -1, (short  *)0,
  324.                              (short  *)0)) {
  325.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  326.                           break;
  327.                           } /*if*/
  328.  
  329.                        if (odefin(SEL_CURS, 8, (char  *)emp_deptno,
  330.                              sizeof(emp_deptno),
  331.                              NULLTERM, -1, (short  *)&ind_emp_deptno,
  332.                              (char  *)0, -1, -1, (short  *)0,
  333.                              (short  *)0)) {
  334.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  335.                           break;
  336.                           } /*if*/
  337.                        } /*if*/
  338.  
  339.  
  340.                     /* execute the query to get a new active set */
  341.                     if (oexec(SEL_CURS)) {
  342.                        ProcessOracleErrorCode(hDlg, SEL_CURS);
  343.                        break;
  344.                        } /*if*/
  345.  
  346.                     session.opened = TRUE;
  347.                     session.nomore = FALSE;
  348.  
  349.                 case ID_EMPLOYEES_FETCH :
  350.  
  351.                     if (session.opened == FALSE) {
  352.                         MessageBox(hDlg, "No active set", "ORACLE", MB_OK);
  353.                         break;
  354.                         } /*if*/
  355.  
  356.                     if (session.nomore == TRUE) {
  357.                         MessageBox(hDlg, "No more data", "ORACLE", MB_OK);
  358.                         break;
  359.                         } /*if*/
  360.  
  361.                     if (ofetch(SEL_CURS)) {
  362.                        if ((SEL_CURS)->rc != NO_MORE_DATA) {
  363.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  364.                           break;
  365.                           } /*if*/
  366.                        else {
  367.                           session.nomore = TRUE;
  368.                           MessageBox(hDlg, "No more data", "ORACLE", MB_OK);
  369.                           break;
  370.                           } /*else*/
  371.                        } /*if*/
  372.  
  373.                     SetEmpRecord(hDlg);
  374.  
  375.                     break;
  376.  
  377.                 case ID_EMPLOYEES_INSERT :
  378.  
  379.                     /* Get current contents of the dialog box */
  380.                     GetEmpRecord(hDlg);
  381.  
  382.                     /* open a cursor for the INSERT */
  383.                     if (oopen(DML_CURS, LDA, (char  *)0,
  384.                            -1, -1, (char  *)0, -1))  {
  385.                        ProcessOracleErrorCode(hDlg, SEL_CURS);
  386.                        break;
  387.                        } /*if*/
  388.  
  389.                     /* parse the INSERT SQL statement */
  390.                     if (oparse(DML_CURS, (char  *)insert, (sb4)-1, (sword)0, (ub4)1)) {
  391.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  392.                         break;
  393.                         } /*if*/
  394.  
  395.                     /* Bind the 8 columns in the emp table to their respective
  396.                        host variables where the data to be inserted resides */
  397.                     if (obndrv(DML_CURS, (char  *)":empno", -1,
  398.                            (char  *)emp_empno, sizeof(emp_empno),
  399.                            NULLTERM, -1, (short  *)&ind_emp_empno,
  400.                            (char  *)0, -1, -1)) {
  401.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  402.                         break;
  403.                         } /*if*/
  404.  
  405.                     if (obndrv(DML_CURS, (char  *)":ename", -1,
  406.                            (char  *)emp_ename, sizeof(emp_ename),
  407.                            NULLTERM, -1, (short  *)&ind_emp_ename,
  408.                            (char  *)0, -1, -1)) {
  409.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  410.                         break;
  411.                         } /*if*/
  412.  
  413.                     if (obndrv(DML_CURS, (char  *)":job", -1,
  414.                            (char  *)emp_job, sizeof(emp_job),
  415.                            NULLTERM, -1, (short  *)&ind_emp_job,
  416.                            (char  *)0, -1, -1)) {
  417.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  418.                         break;
  419.                         } /*if*/
  420.  
  421.                     if (obndrv(DML_CURS, (char  *)":mgr", -1,
  422.                            (char  *)emp_mgr, sizeof(emp_mgr),
  423.                            NULLTERM, -1, (short  *)&ind_emp_mgr,
  424.                            (char  *)0, -1, -1)) {
  425.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  426.                         break;
  427.                         } /*if*/
  428.  
  429.                     if (obndrv(DML_CURS, (char  *)":hiredate", -1,
  430.                            (char  *)emp_hiredate, sizeof(emp_hiredate),
  431.                            NULLTERM, -1, (short  *)&ind_emp_hiredate,
  432.                            (char  *)0, -1, -1)) {
  433.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  434.                         break;
  435.                         } /*if*/
  436.  
  437.                     if (obndrv(DML_CURS, (char  *)":sal", -1,
  438.                            (char  *)emp_sal, sizeof(emp_sal),
  439.                            NULLTERM, -1, (short  *)&ind_emp_sal,
  440.                            (char  *)0, -1, -1)) {
  441.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  442.                         break;
  443.                         } /*if*/
  444.  
  445.                     if (obndrv(DML_CURS, (char  *)":comm", -1,
  446.                            (char  *)emp_comm, sizeof(emp_comm),
  447.                            NULLTERM, -1, (short  *)&ind_emp_comm,
  448.                            (char  *)0, -1, -1)) {
  449.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  450.                         break;
  451.                         } /*if*/
  452.  
  453.                     if (obndrv(DML_CURS, (char  *)":deptno", -1,
  454.                            (char  *)emp_deptno, sizeof(emp_deptno),
  455.                            NULLTERM, -1, (short  *)&ind_emp_deptno,
  456.                            (char  *)0, -1, -1)) {
  457.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  458.                         break;
  459.                         } /*if*/
  460.  
  461.                     /* insert the row */
  462.                     if (oexec(DML_CURS)) {
  463.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  464.                         break;
  465.                         } /*if*/
  466.  
  467.                     /* close the cursor */
  468.                     if (oclose(DML_CURS)) {
  469.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  470.                         break;
  471.                         } /*if*/
  472.  
  473.                     break;
  474.  
  475.                 case ID_EMPLOYEES_UPDATE :
  476.  
  477.                     if (session.opened == FALSE) {
  478.                         MessageBox(hDlg, "No active set", "ORACLE", MB_OK);
  479.                         break;
  480.                         } /*if*/
  481.  
  482.                     /* Get current data from dialog box */
  483.                     GetEmpRecord(hDlg);
  484.  
  485.                     /* open a cursor for the UPDATE */
  486.                     if (oopen(DML_CURS, LDA, (char  *)0,
  487.                            -1, -1, (char  *)0, -1))  {
  488.                        ProcessOracleErrorCode(hDlg, SEL_CURS);
  489.                        break;
  490.                        } /*if*/
  491.  
  492.                     /* parse the UPDATE SQL statement */
  493.                     if (oparse(DML_CURS, (char  *)update, (sb4)-1, (sword)0, (ub4)1)) {
  494.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  495.                         break;
  496.                         } /*if*/
  497.  
  498.                     /* Bind the host variable ":row_id" to the rowid in the
  499.                          cursor used for the select statement */
  500.                     if (obndrv(DML_CURS, (char  *)":row_id", -1,
  501.                            (char  *)&((SEL_CURS)->rid), 16, ROWID,
  502.                            -1, (short  *)0, (char  *)0, -1, -1)) {
  503.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  504.                         break;
  505.                         } /*if*/
  506.  
  507.                     /* Bind the 8 columns in the emp table to their respective
  508.                        host variables where the updated data resides */
  509.                     if (obndrv(DML_CURS, (char  *)":empno", -1,
  510.                            (char  *)emp_empno, sizeof(emp_empno),
  511.                            NULLTERM, -1, (short  *)&ind_emp_empno,
  512.                            (char  *)0, -1, -1)) {
  513.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  514.                         break;
  515.                         } /*if*/
  516.  
  517.                     if (obndrv(DML_CURS, (char  *)":ename", -1,
  518.                            (char  *)emp_ename, sizeof(emp_ename),
  519.                            NULLTERM, -1, (short  *)&ind_emp_ename,
  520.                            (char  *)0, -1, -1)) {
  521.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  522.                         break;
  523.                         } /*if*/
  524.  
  525.                     if (obndrv(DML_CURS, (char  *)":job", -1,
  526.                            (char  *)emp_job, sizeof(emp_job),
  527.                            NULLTERM, -1, (short  *)&ind_emp_job,
  528.                            (char  *)0, -1, -1)) {
  529.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  530.                         break;
  531.                         } /*if*/
  532.  
  533.                     if (obndrv(DML_CURS, (char  *)":mgr", -1,
  534.                            (char  *)emp_mgr, sizeof(emp_mgr),
  535.                            NULLTERM, -1, (short  *)&ind_emp_mgr,
  536.                            (char  *)0, -1, -1)) {
  537.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  538.                         break;
  539.                         } /*if*/
  540.  
  541.                     if (obndrv(DML_CURS, (char  *)":hiredate", -1,
  542.                            (char  *)emp_hiredate, sizeof(emp_hiredate),
  543.                            NULLTERM, -1, (short  *)&ind_emp_hiredate,
  544.                            (char  *)0, -1, -1)) {
  545.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  546.                         break;
  547.                         } /*if*/
  548.  
  549.                     if (obndrv(DML_CURS, (char  *)":sal", -1,
  550.                            (char  *)emp_sal, sizeof(emp_sal),
  551.                            NULLTERM, -1, (short  *)&ind_emp_sal,
  552.                            (char  *)0, -1, -1)) {
  553.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  554.                         break;
  555.                         } /*if*/
  556.  
  557.                     if (obndrv(DML_CURS, (char  *)":comm", -1,
  558.                            (char  *)emp_comm, sizeof(emp_comm),
  559.                            NULLTERM, -1, (short  *)&ind_emp_comm,
  560.                            (char  *)0, -1, -1)) {
  561.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  562.                         break;
  563.                         } /*if*/
  564.  
  565.                     if (obndrv(DML_CURS, (char  *)":deptno", -1,
  566.                            (char  *)emp_deptno, sizeof(emp_deptno),
  567.                            NULLTERM, -1, (short  *)&ind_emp_deptno,
  568.                            (char  *)0, -1, -1)) {
  569.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  570.                         break;
  571.                         } /*if*/
  572.  
  573.                     /* update the row */
  574.                     if (oexec(DML_CURS)) {
  575.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  576.                         break;
  577.                         } /*if*/
  578.  
  579.                     /* close the cursor */
  580.                     if (oclose(DML_CURS)) {
  581.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  582.                         break;
  583.                         } /*if*/
  584.  
  585.                     break;
  586.  
  587.                 case ID_EMPLOYEES_DELETE :
  588.  
  589.                     if (session.opened == FALSE) {
  590.                         MessageBox(hDlg, "No active set", "ORACLE", MB_OK);
  591.                         break;
  592.                         } /*if*/
  593.  
  594.                     /* open a cursor for the DELETE */
  595.                     if (oopen(DML_CURS, LDA, (char  *)0,
  596.                            -1, -1, (char  *)0, -1))  {
  597.                        ProcessOracleErrorCode(hDlg, SEL_CURS);
  598.                        break;
  599.                        } /*if*/
  600.  
  601.                     /* parse the DELETE SQL statement */
  602.                     if (oparse(DML_CURS, (char  *)delete, (sb4)-1, (sword)0, (ub4)1)) {
  603.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  604.                         break;
  605.                         } /*if*/
  606.  
  607.                     /* Bind the host variable ":row_id" to the rowid in the
  608.                          cursor used for the select statement */
  609.                     if (obndrv(DML_CURS, (char  *)":row_id", -1,
  610.                            (char *)&((SEL_CURS)->rid), 16, ROWID,
  611.                            -1, (short  *)0, (char  *)0, -1, -1)) {
  612.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  613.                         break;
  614.                         } /*if*/
  615.  
  616.                     /* delete the row */
  617.                     if (oexec(DML_CURS)) {
  618.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  619.                         break;
  620.                         } /*if*/
  621.  
  622.                     /* close the cursor */
  623.                     if (oclose(DML_CURS)) {
  624.                         ProcessOracleErrorCode(hDlg, DML_CURS);
  625.                         break;
  626.                         } /*if*/
  627.  
  628.                     break;
  629.  
  630.                 case ID_EMPLOYEES_CLEAR :
  631.                     emp_empno[ 0 ] = EOS;
  632.                     emp_ename[ 0 ] = EOS;
  633.                     emp_job[ 0 ] = EOS;
  634.                     emp_mgr[ 0 ] = EOS;
  635.                     emp_hiredate[ 0 ] = EOS;
  636.                     emp_sal[ 0 ] = EOS;
  637.                     emp_comm[ 0 ] = EOS;
  638.                     emp_deptno[ 0 ] = EOS;
  639.                     SetEmpRecord(hDlg);
  640.                     break;
  641.  
  642.                 case ID_EMPLOYEES_COMMIT :
  643.  
  644.                     /* Commit work done so far. */
  645.                     if (ocom(LDA))
  646.                         ProcessOracleErrorCode(hDlg, LDA);
  647.  
  648.                     /* COMMIT frees locks so close the SELECT. */
  649.                     if (session.opened == TRUE)
  650.                        if (oclose(SEL_CURS))
  651.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  652.                        else {
  653.                           session.opened = FALSE;
  654.                           session.nomore = TRUE;
  655.                           } /*else*/
  656.  
  657.                     break;
  658.  
  659.                 case ID_EMPLOYEES_ROLLBACK :
  660.  
  661.                     /* Rollback work done so far. */
  662.                     if (orol(LDA))
  663.                         ProcessOracleErrorCode(hDlg, LDA);
  664.  
  665.                     /* ROLLBACK frees locks so close the SELECT. */
  666.                     if (session.opened == TRUE)
  667.                        if (oclose(SEL_CURS))
  668.                           ProcessOracleErrorCode(hDlg, SEL_CURS);
  669.                        else {
  670.                           session.opened = FALSE;
  671.                           session.nomore = TRUE;
  672.                           } /*else*/
  673.  
  674.                     break;
  675.  
  676.                 default :
  677.                     return FALSE;
  678.                 } /*switch*/
  679.  
  680.             break;
  681.  
  682.         case WM_INITDIALOG :
  683.             break;
  684.  
  685.         default :
  686.             return FALSE;
  687.         } /*switch*/
  688.  
  689.     return TRUE;
  690.  
  691. } /*EmployeesDlg*/
  692.  
  693. /*
  694.  *DoEmployees
  695.  */
  696. PROCEDURE DoEmployees(hInst, hWnd)
  697. HANDLE hInst;
  698. HWND hWnd;
  699. {
  700.  
  701.     DialogBox(hInst, MAKEINTRESOURCE(EMPLOYEESBOX), hWnd, EmployeesDlg);
  702.     return;
  703.  
  704. } /*DoEmployees*/
  705.  
  706. /*
  707.  *WinMain
  708.  */
  709. int WINAPI WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  710. HANDLE hInstance, hPrevInstance;
  711. LPSTR lpszCmdLine;
  712. int cmdShow;
  713.  
  714. {
  715.     WNDCLASS SampleClass;
  716.     HMENU hmenuSystem, hmenuSample;
  717.     HWND hWnd;
  718.     MSG msg;
  719.     char szAbout[ MAX_ABOUT_STRING + 1 ];
  720.     char szAppName[ MAX_APPNAME_STRING + 1 ];
  721.     char szSampleMenu[ MAX_SAMPLEMENU_STRING + 1 ];
  722.     char szTitle[ MAX_TITLE_STRING + 1 ];
  723.  
  724.     /* Lock our data segment, in case we are a small or medium model program.
  725.         If not, it is possible that some of the  pointers that are passed
  726.         in to ora6win.dll via OCI might become invalid. */
  727.     LockSegment(-1);
  728.  
  729.     /* Load strings from resource */
  730.     LoadString(hInstance, IDS_ABOUT, szAbout, MAX_ABOUT_STRING);
  731.     LoadString(hInstance, IDS_APPNAME, szAppName, MAX_APPNAME_STRING);
  732.     LoadString(hInstance, IDS_SAMPLEMENU,
  733.                                 szSampleMenu, MAX_SAMPLEMENU_STRING);
  734.     LoadString(hInstance, IDS_TITLE, szTitle, MAX_TITLE_STRING);
  735.  
  736.     if (!hPrevInstance) {
  737.         SampleClass.style = CS_HREDRAW | CS_VREDRAW;
  738.         SampleClass.lpfnWndProc = (WNDPROC)SampleWndProc;
  739.         SampleClass.cbClsExtra = 0;
  740.         SampleClass.cbWndExtra = 0;
  741.         SampleClass.hInstance = hInstance;
  742.         SampleClass.hIcon = LoadIcon(hInstance, szAppName);
  743.         SampleClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  744.         SampleClass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
  745.         SampleClass.lpszMenuName = szSampleMenu;
  746.         SampleClass.lpszClassName = szAppName;
  747.  
  748.         if (!RegisterClass((LPWNDCLASS)&SampleClass))
  749.             return FALSE;
  750.  
  751.         } /*if*/
  752.  
  753.     /* Initialize the current connection structure. */
  754.     session.connected = FALSE;
  755.     session.opened = FALSE;
  756.     session.nomore = TRUE;
  757.  
  758.     hInst = hInstance;
  759.  
  760.     hmenuSample = LoadMenu(hInst, "SAMPLEMENU");
  761.  
  762.     hWnd = CreateWindow(szAppName,
  763.                         szTitle,
  764.                         WS_OVERLAPPEDWINDOW,
  765.                         CW_USEDEFAULT,
  766.                         CW_USEDEFAULT,
  767.                         CW_USEDEFAULT,
  768.                         CW_USEDEFAULT,
  769.                         (HWND)NULL,
  770.                         (HMENU)NULL,
  771.                         (HANDLE)hInst,
  772.                         NULL);
  773.  
  774.     hmenuSystem = GetSystemMenu(hWnd, FALSE);
  775.     InsertMenu(hmenuSystem, 0, MF_BYPOSITION | MF_STRING, IDS_ABOUT,
  776.                                                             szAbout);
  777.     ShowWindow(hWnd, SW_SHOWNORMAL);
  778.     UpdateWindow(hWnd);
  779.  
  780.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  781.         TranslateMessage((LPMSG)&msg);
  782.         DispatchMessage((LPMSG)&msg);
  783.         } /*while*/
  784.  
  785.     return( (int)msg.wParam );
  786.  
  787. } /*WinMain*/
  788.  
  789. /*
  790.  *SampleWndProc
  791.  */
  792. long CALLBACK SampleWndProc(hWnd, message, wParam, lParam)
  793. HWND hWnd;
  794. unsigned message;
  795. WORD wParam;
  796. LONG lParam;
  797. {
  798.     static HWND hInst;
  799.     HMENU hmenuSample;
  800.  
  801.     switch (message) {
  802.  
  803.         case WM_CREATE :
  804.             hInst = ((LPCREATESTRUCT) lParam)->hInstance;
  805.             return TRUE;
  806.  
  807.         case WM_SYSCOMMAND :
  808.             switch (wParam) {
  809.                 case IDS_ABOUT:
  810.                     DialogBox(hInst, MAKEINTRESOURCE(ABOUTBOX),
  811.                             hWnd, AboutDlg);
  812.                     return TRUE;
  813.                 default:
  814.                     break;
  815.                 } /*switch*/
  816.             break;
  817.  
  818.         case WM_COMMAND :
  819.             if (LOWORD(lParam) == 0) {    /*message from a menu*/
  820.  
  821.                 switch (wParam) {
  822.  
  823.                     case IDM_EXIT :
  824.                         SendMessage(hWnd, WM_CLOSE, 0, 0L);
  825.                         return TRUE;
  826.  
  827.                     case IDM_ORACLE_CONNECT :
  828.                         if (DoLogon(hInst, hWnd) == TRUE) {
  829.                             hmenuSample = GetMenu(hWnd);
  830.                             EnableMenuItem(hmenuSample, IDM_ORACLE_CONNECT,
  831.                                                                   MF_GRAYED);
  832.                             EnableMenuItem(hmenuSample, IDM_ORACLE_DISCONNECT,
  833.                                                                   MF_ENABLED);
  834.                             EnableMenuItem(hmenuSample, IDM_ORACLE_EMPLOYEES,
  835.                                                                   MF_ENABLED);
  836.                             } /*if*/
  837.                         return TRUE;
  838.  
  839.                     case IDM_ORACLE_DISCONNECT :
  840.                         if (DoDisconnect(hWnd) == TRUE) {
  841.                             hmenuSample = GetMenu(hWnd);
  842.                             EnableMenuItem(hmenuSample, IDM_ORACLE_CONNECT,
  843.                                                                   MF_ENABLED);
  844.                             EnableMenuItem(hmenuSample, IDM_ORACLE_DISCONNECT,
  845.                                                                   MF_GRAYED);
  846.                             EnableMenuItem(hmenuSample, IDM_ORACLE_EMPLOYEES,
  847.                                                                   MF_GRAYED);
  848.                             } /*if*/
  849.                         return TRUE;
  850.  
  851.                     case IDM_ORACLE_EMPLOYEES :
  852.                         DoEmployees(hInst, hWnd);
  853.                         return TRUE;
  854.  
  855.                     default :
  856.                         break;
  857.                     } /*switch*/
  858.                 } /*if*/
  859.             break;
  860.  
  861.         case WM_CLOSE :
  862.             /* Disconnect before terminating Windows application. */
  863.             if (session.connected == TRUE)
  864.                 DoDisconnect(hWnd);
  865.  
  866.             break; /* Let this pass thru so default operation can occur. */
  867.  
  868.         case WM_DESTROY :
  869.             PostQuitMessage(0);
  870.             break;
  871.  
  872.         default:
  873.             break;
  874.         } /*switch*/
  875.  
  876.     /* Performs default operation. */
  877.     return( DefWindowProc(hWnd, message, wParam, lParam) );
  878.  
  879. } /*SampleWndProc*/
  880.  
  881. /*
  882.  *LogonDlg
  883.  */
  884. BOOL CALLBACK LogonDlg(hDlg, message, wParam, lParam)
  885. HWND hDlg;
  886. unsigned message;
  887. WORD wParam;
  888. LONG lParam;
  889. {
  890.  
  891.     switch (message) {
  892.  
  893.         case WM_COMMAND :
  894.  
  895.             switch (wParam) {
  896.  
  897.                 case ID_LOGON_OK :
  898.                     GetDlgItemText(hDlg, ID_LOGON_USERID,
  899.                                        userid, MAX_USERID);
  900.                     GetDlgItemText(hDlg, ID_LOGON_PASSWORD,
  901.                                        password, MAX_PASSWORD);
  902.                     GetDlgItemText(hDlg, ID_LOGON_CONNECT,
  903.                                        connectstr, MAX_CONNECT);
  904.                     EndDialog(hDlg, 1);
  905.                     return TRUE;
  906.  
  907.                 case ID_LOGON_CANCEL :
  908.                     EndDialog(hDlg, 0);
  909.                     return TRUE;
  910.  
  911.                 default :
  912.                     break;
  913.                 } /*switch*/
  914.  
  915.             break;
  916.  
  917.         case WM_INITDIALOG :
  918.             return TRUE;
  919.  
  920.         default :
  921.             return FALSE;
  922.         } /*switch*/
  923.  
  924.     return FALSE;
  925.  
  926. } /*LogonDlg*/
  927.  
  928. /*
  929.  *DoLogon
  930.  */
  931. int DoLogon(hInst, hWnd)
  932. HANDLE hInst;
  933. HWND hWnd;
  934. {
  935.     char connstr[ MAX_USERID + MAX_CONNECT + 2 ];
  936.                                             /* used to store userid@database */
  937.  
  938.     if (session.connected == TRUE)
  939.         return TRUE;
  940.  
  941.     while (session.connected == FALSE) {
  942.  
  943.         /* Give the Logon Dialog Box -- control passes to LogonDlg(). */
  944.         if (DialogBox(hInst, MAKEINTRESOURCE( LOGONBOX ), hWnd,
  945.                       LogonDlg) == 0)
  946.             break;
  947.  
  948.         /* Append host string to username. */
  949.         strcpy(connstr, userid);
  950.         if (strlen(connectstr) != 0) {
  951.             strcat(connstr, "@");
  952.             strcat(connstr, connectstr);
  953.             } /*if*/
  954.  
  955.         /* Call orlon() to do connect. */
  956.         if (olon(LDA, (char  *)connstr, -1,
  957.                    (char  *)password, -1, 0)) {
  958.             ProcessOracleErrorCode(hWnd, LDA);
  959.             if (MessageBox(hWnd, "Login to ORACLE",
  960.                                   "ORACLE", MB_RETRYCANCEL) != IDRETRY)
  961.                 break;
  962.             } /*if*/
  963.         else {
  964.             MessageBox(hWnd, "Logon Successful", "ORACLE", MB_OK);
  965.             session.connected = TRUE;
  966.             } /*else*/
  967.  
  968.         } /*while*/
  969.  
  970.     return( session.connected );
  971.  
  972. } /*DoLogon*/
  973.  
  974. /*
  975.  *DoDisconnect
  976.  */
  977. int DoDisconnect(hWnd)
  978. HWND hWnd;
  979. {
  980.     if (session.connected == FALSE)
  981.         return TRUE;
  982.  
  983.     if (session.opened == TRUE)
  984.         if (oclose(SEL_CURS))
  985.            ProcessOracleErrorCode(hWnd, SEL_CURS);
  986.         else {
  987.            session.opened = FALSE;
  988.            session.nomore = TRUE;
  989.            } /*else*/
  990.  
  991.     /* Commits the current transaction and releases the current connection.*/
  992.  
  993.     /* Commit work done so far. */
  994.     if (ocom(LDA))
  995.         ProcessOracleErrorCode(hWnd, LDA);
  996.     else
  997.         MessageBox(hWnd, "Commit Successful", "ORACLE", MB_OK);
  998.  
  999.     /* Disconnect the current connection. */
  1000.     if (ologof(LDA))
  1001.         ProcessOracleErrorCode(hWnd, LDA);
  1002.     else {
  1003.         MessageBox(hWnd, "Disconnect Successful", "ORACLE", MB_OK);
  1004.         session.connected = FALSE;
  1005.         } /*else*/
  1006.  
  1007.     return((session.connected == FALSE) ? TRUE : FALSE);
  1008.  
  1009. } /*DoDisconnect*/
  1010.